import 'package:flutter/cupertino.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_shop/config/httpHeader.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:url_launcher/url_launcher.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> with AutomaticKeepAliveClientMixin {
  // 混入AutomaticKeepAliveClientMixin可以保持页面状态并重写wantKeepAlive => true
  @override
  bool get wantKeepAlive => true;

  final String text = '还没有数据';
  @override
  Widget build(BuildContext context) {
    print('设备像素密度${ScreenUtil().pixelRatio}');
    print('设备高${ScreenUtil().screenHeight}');
    print('设备宽${ScreenUtil().screenWidth}');
    return Center(
      child: Column(
        children: <Widget>[
          Container(
            height: ScreenUtil().setWidth(400),
            child: new Swiper(
              // 不能直接放列表中需要放到Container中
              itemBuilder: (BuildContext context, int index) {
                return Image.network(
                  'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3887220784,400144123&fm=26&gp=0.jpg',
                  fit: BoxFit.fill,
                );
              },
              containerWidth: ScreenUtil().setWidth(750),
              // control: new SwiperControl(),
              itemCount: 4,
              autoplay: true,
              pagination: SwiperPagination(),
            ),
          ),
          RaisedButton(
            onPressed: () {
              getHttp().then((value) {
                print('开始请求了');
                print(value['error']['msg']);
              });
            },
            child: Text(
              '点击请求',
              style: TextStyle(
                fontSize: ScreenUtil().setSp(
                  32,
                  allowFontScalingSelf: false,
                ), // false不随设备尺寸变化而变化
              ),
            ),
          ),
          RaisedButton(
            onPressed: () async {
              const url = 'tel:13506258004'; // 打电话
              // const url = 'tel:https://flutter.cn'; // 打开网页
              if (await canLaunch(url)) {
                await launch(url);
              } else {
                throw 'Could not launch $url';
              }
            },
            child: Text('拨打电话'),
          ),
        ],
      ),
    );
  }

  Future getHttp() async {
    try {
      Response response;
      Dio dio = new Dio();
      dio.options.headers = httpHeader;
      response = await dio.post('https://time.geekbang.org/serv/v1/my/data');
      return response.data;
    } catch (err) {
      return err;
    }
  }
}
